//支付点击事件
var userMess="";
function payType(orderId,totalFee){
    getUser();
    userMess=JSON.stringify(ym.user);
    if (is_wechat_client()){
        payJsh(orderId,totalFee);
    }else {
        $("#alertModal .am-modal-bd").html("请在微信内打开该界面！");
        $("#alertModal").modal();
        setTimeout(function () {
            $("#alertModal").modal('close');
            $(".masks").addClass("hide");
        }, 2000);
    }
}
//判断是微信浏览器还是其他浏览器
function is_wechat_client() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true;
    } else {
        return false;
    }
}

//获取后台接口返回值
function payJsh(orderId,totalFee){
    $.ajax({
        url:'/web/shop/wechatPay',
        data:{
            orderId:orderId,
            ymUser:userMess,
            totalcost:totalFee
        },
        success:function(data){
            pay(data.obj);
        }
    });
}
//唤起微信支付
function pay(data) {
    if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
    } else {
        onBridgeReady(data);
    }
}

/*/开始支付*/
function onBridgeReady(data) {
    var timeStamp = data.timeStamp;
    var nonceStr = data.nonceStr;
    var pg = data.pg;
    var signType = data.signType;
    var paySign = data.paySign;
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest', {
            "appId": "wxfe0300a14724e3ea",     //公众号名称，由商户传入
            "timeStamp": timeStamp,         //时间戳，自1970年以来的秒数
            "nonceStr": nonceStr, //随机串
            "package": pg,
            "signType": signType,         //微信签名方式:
            "paySign": paySign    //微信签名
        }, function (res) {
            if (res.err_msg == "get_brand_wcpay_request:ok") {
                $(".masks").addClass("hide");
                $("#alertModal .am-modal-bd").html("购买成功！");
                $("#alertModal").modal();
                setTimeout(function () {
                    $("#alertModal").modal('close');
                    window.location.href="order.html";
                }, 2000);
            } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                $(".masks").addClass("hide");
                $("#alertModal .am-modal-bd").html("购买失败！");
                $("#alertModal").modal();
                setTimeout(function () {
                    $("#alertModal").modal('close');
                }, 2000);
            } else {
                //支付失败
                $(".masks").addClass("hide");
                $("#alertModal .am-modal-bd").html("购买失败！");
                $("#alertModal").modal();
                setTimeout(function () {
                    $("#alertModal").modal('close');
                }, 2000);
            }
        }
    );
}